<?php

/*
 $xmlData = array(
    'xml' => array(
        '~attr' => array(
            'xsi:schemaLocation' => 'http://example.com/schema.xsd',
            'xmlns' => 'http://example.com/envelope'
        ),
        'EnvelopeVersion' => '1.0',
        'FormHeader' => array(
            'FormIdentifier' => 'AA29',
            'SubmissionNumber' => 601),
        'Form' => array(
            'CompanyName' => 'TotoSoft',
            '~array:Applicant' => array('Anton Homenko', 'Ivan Rednikov'),
            'IngoreMe' => '~ignore'
        )
    )
);

$xml = new DOMDocument("1.0", "UTF-8");
$xml->formatOutput = true;
xmlAddElementsFromArr($xml, $xml, $xmlData);
$xmlStr = $xml->saveXML();

echo "<pre>";
print_r($xmlStr);
echo "";
 *
 * 
 */


function xmlBoolean($value)
{
    return (isset($value) && $value) ? 'true' : 'false';
}

function valueByKeyOrIgnore($ar, $key)
{
    return (isset($ar[$key]) && $ar[$key] != '')? $ar[$key] : '~ignore';
}

function valueOrIgnore($condition, $value)
{
    return ($condition)? $value : '~ignore';
}

function issetValueOrIgnore(&$value)
{
    return (isset($value)) ? $value : '~ignore';
}

function arrayWithItemsOrIgnore($ar)
{
    return (is_array($ar) && count($ar) > 0)? $ar : '~ignore';
}


function xmlCreateAttributes($xmlDoc, $xmlElement, $attributes)
{
    foreach($attributes as $attrName => $attrValue)
    {
        $xsi = $xmlDoc->createAttribute($attrName);
        $xmlElement->appendChild($xsi);
        $xsi->appendChild($xmlDoc->CreateTextNode($attrValue));
    }
}

function xmlCreateElement($xmlDoc, $xmlParent, $elemName, $elemText = false,
    $attributes = array())
{
    // Create element
    $elem = $xmlDoc->createElement($elemName);
    $xmlParent->appendChild($elem);

    // Set text contents if any
    if ($elemText !== false)
        $elem->appendChild($xmlDoc->createTextNode($elemText));

    // Set attributes
    if (count($attributes) > 0)
        xmlCreateAttributes($xmlDoc, $elem, $attributes);
    return $elem;
}

function xmlAddElementsFromArr($xmlDoc, $xmlParent, $elems)
{
    foreach($elems as $name => $value)
        // Must be ignored
        if($value == '~ignore')
            continue;

        // Attributes specified
        elseif ($name == '~attr' && is_array($value))
            xmlCreateAttributes($xmlDoc, $xmlParent, $value);

        // Array of elements with the same name
        elseif(strpos($name, '~array:') !== false && is_array($value))
        {
            $elName = str_replace('~array:', '', $name);
            foreach($value as $val)
                xmlAddElementsFromArr($xmlDoc, $xmlParent, array($elName => $val));
        }

        // Simple child element with text value inside
        elseif (is_string($value) || is_numeric($value))
            xmlCreateElement($xmlDoc, $xmlParent, $name, $value);

        // Simle child element with no value inside
        elseif($value === false)
            xmlCreateElement($xmlDoc, $xmlParent, $name);

        // Call recursively for child items
        elseif(is_array($value))
        {
            $elem = xmlCreateElement($xmlDoc, $xmlParent, $name);
            xmlAddElementsFromArr($xmlDoc, $elem, $value);
        }
}
